package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.dictmap.DotDict;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.persistence.dao.DotMapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.business.service.IDotService;
import com.stylefeng.guns.modular.system.warpper.DotWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Dot;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 网点管理控制器
 *
 * @author xiejie
 * @Date 2018-01-15 10:26:35
 */
@Controller
@RequestMapping("/dot")
public class DotController extends BaseController {

    private String PREFIX = "/system/dot/";

    @Resource
    DotMapper dotMapper;

    @Resource
    IDotService iDotService;

    /**
     * 跳转到网点管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "dot.html";
    }

    /**
     * 跳转到添加网点管理
     */
    @RequestMapping("/dot_add")
    public String dotAdd() {
        return PREFIX + "dot_add.html";
    }

    /**
     * 跳转到修改网点管理
     */
    @RequestMapping("/dot_update/{dotId}")
    public String dotUpdate(@PathVariable Integer dotId, Model model) {
        Dot dot = dotMapper.selectById(dotId);
        model.addAttribute("item",dot);
        LogObjectHolder.me().set(dot);
        return PREFIX + "dot_edit.html";
    }

    /**
     * 获取网点管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) Integer deptid,Integer limit, Integer offset    ) {
        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",deptid==null?ShiroKit.getUser().getDeptId():deptid);
        m.put("name",name);
        List<Map<String, Object>> dots = dotMapper.selectDotsList(page,m);
        pageInfoBT.setRows(dots);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 获取网点管理列表
     */
    @RequestMapping(value = "/searchList")
    @ResponseBody
    public Object searchList() {
        EntityWrapper<Dot> dotEntityWrapper=new EntityWrapper<>();
        List<Dot> dots=iDotService.selectList(dotEntityWrapper);
        Map map=new HashMap();
        map.put("message","");
        map.put("code","200");
        map.put("redirect","");
        map.put("value",dots);
        return map;
    }

    /**
     * 新增网点管理
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "修改网点管理", key = "dotShortName", dict = DotDict.class)
    @ResponseBody
    public Object add(Dot dot) {
        dot.setDeptid(ShiroKit.getUser().getDeptId());
        return iDotService.insertDot(dot);
    }

    /**
     * 删除网点管理
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "修改网点管理", key = "dotShortName", dict = DotDict.class)
    @ResponseBody
    public Object delete(@RequestParam Integer dotId) {
        dotMapper.deleteById(dotId);
        return SUCCESS_TIP;
    }

    /**
     * 修改网点管理
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改网点管理", key = "dotShortName", dict = DotDict.class)
    @ResponseBody
    public Object update(Dot dot) {
      //  dotMapper.updateById(dot);
        return iDotService.updateDot(dot);
    }

    /**
     * 网点管理详情
     */
    @RequestMapping(value = "/detail/{dotId}")
    @ResponseBody
    public Object detail(@PathVariable("dotId") Integer dotId) {
        return dotMapper.selectById(dotId);
    }
}
